package client

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/client"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var DramaGroup = core.Group("/client", newDramaHandler, regDrama, middleware.TokenAuth())

func newDramaHandler(srv client.IDramaService) *dramaHandler {
	return &dramaHandler{srv: srv}
}

func regDrama(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *dramaHandler) {
		rg.GET("/drama/all", handle.all)
		rg.GET("/drama/list", handle.list)
		rg.GET("/drama/detail", handle.detail)
		rg.POST("/drama/add", handle.add)
		rg.POST("/drama/edit", handle.edit)
		rg.POST("/drama/del", handle.del)
	})
}

type dramaHandler struct {
	srv client.IDramaService
}

// list drama列表
func (hd dramaHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.DramaListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail drama详情
func (hd dramaHandler) detail(c *gin.Context) {
	var detailReq req.DramaDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.DramaId)
	response.CheckAndRespWithData(c, res, err)
}

// add drama新增
func (hd dramaHandler) add(c *gin.Context) {
	var addReq req.DramaAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(addReq))
}

// edit drama编辑
func (hd dramaHandler) edit(c *gin.Context) {
	var editReq req.DramaEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}

	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

// del drama删除
func (hd dramaHandler) del(c *gin.Context) {
	var delReq req.DramaDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.DramaId))
}

// all 角色所有
func (rh dramaHandler) all(c *gin.Context) {
	res, err := rh.srv.All()
	response.CheckAndRespWithData(c, res, err)
}
